package com.wu.neihan.servlet;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wu.neihan.model.Gather;
import com.wu.neihan.service.CommentQueueService;
import com.wu.neihan.service.PostQueueService;
import com.wu.neihan.service.QueueProduce;
import com.wu.neihan.service.QueueService;
import com.wu.neihan.service.UserQueueService;
import com.wu.util.ConMysql;
import com.wu.util.HttpUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * Servlet implementation class MainContral
 */
@WebServlet(name = "MainContral", urlPatterns = "/MainContral", loadOnStartup = 1)
public class MainContral extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	public void init() throws ServletException {
		//ConMysql.getCon();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		if (request.getParameter("type").equals("begin")) {
			begin(request, response);
		} else {
//			List<Gather> id = null;
//			try {
//				id = ConMysql.selectSql("select * from mto_gather order by type,last_date DESC");
//			} catch (Exception e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
			String body="";
			String fileName="D:\\fm_log.log";
			 File file = new File(fileName);
		        try {
		        	 if(file.isFile() && file.exists()){ //判断文件是否存在
		                    InputStreamReader read = new InputStreamReader(new FileInputStream(file),"utf-8");//考虑到编码格式
		                    BufferedReader bufferedReader = new BufferedReader(read);
					String lineTxt = null;
		                    while((lineTxt = bufferedReader.readLine()) != null){
		                    	body=body+lineTxt+"<br/>";
		                    }
		                    read.close();
		        }else{
		            System.out.println("找不到指定的文件");
		        }
		        } catch (Exception e) {
		            System.out.println("读取文件内容出错");
		            e.printStackTrace();
		        }
		        
			response.setCharacterEncoding("UTF-8");
			response.getWriter().append(body);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

	public void begin(HttpServletRequest request, HttpServletResponse response) throws IOException {
		new QueueProduce().start();
//		for(int i=0;i<Integer.valueOf(request.getParameter("user").toString());i++){
//			new UserQueueService().start();
//		}
		for(int i=0;i<Integer.valueOf(request.getParameter("post").toString());i++){
			new PostQueueService().start();
		}
//		for(int i=0;i<Integer.valueOf(request.getParameter("comments").toString());i++){
//			new CommentQueueService().start();
//		}
		response.sendRedirect("index.jsp");
	}

	@Override
	public void destroy() {
		//ConMysql.closeCon();
	}
	
}
